package com.lyt.service;

import com.alibaba.fastjson.JSON;
import com.lyt.bean.*;
import com.lyt.dao.PaperDao;
import com.lyt.dao.QuestionDao;
import com.lyt.dao.TestExamDao;
import com.lyt.util.BeanUtil;
import com.lyt.vo.ItemVo;
import com.lyt.vo.PaperVo;
import com.lyt.vo.QuestionVo;
import com.lyt.vo.QuestionsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lyt
 * @Date: 2023/05/21/17:57
 * @Description:
 */
@Service
public class TestService {

    @Autowired
    TestExamDao testExamDao;
    @Autowired
    QuestionDao questionDao;

    @Autowired
    PaperDao paperDao;

    @RequestMapping("/testExam/get")//根据学生id获取 试卷
    public List<PaperVo> getPaperById(int courseId, int grade) {

        List<PublishPaper> paperUsers = testExamDao.getTestPapers(courseId, grade);
        List<PaperVo> paperVos = new ArrayList<>();
        for (PublishPaper paperUser : paperUsers) {

            paperVos.add(getTestPaper(paperUser.getPaperId()));
        }

        return paperVos;
    }


    public PaperVo getTestPaper(int id) {
        //id是用户的id
        Paper paper = paperDao.selectPaperByPaperId(id);

        System.out.println("打印一下查询到的paper " + paper);
        PaperVo paperVo = new PaperVo();
        //转换为paperVo
        BeanUtil.beanConvert(paper, paperVo);
        System.out.println(paperVo);
        //根据paperId获取 试题
        List<PaperQuestion> paperQuestions = questionDao.getPaperQuestionById(id);
//       System.out.println("打印一下 paperQuestions"+paperQuestions);
        //获取所有小题
        List<QuestionVo> questionVos = new ArrayList<>();
        //获取所有的相关题目 并写入 item中
        for (PaperQuestion paperQuestion : paperQuestions) {
            Question question = questionDao.getQuestionById(paperQuestion.getQuestionId());
//           System.out.println("查询到的 问题"+ question);
            //将question转化为vo
            QuestionVo questionVo = new QuestionVo();
            BeanUtil.beanConvert(question, questionVo);
            List<ItemVo> items = (List<ItemVo>) JSON.parse(question.getContent());
            System.out.println("item转换情况：" + items);
            questionVo.setItems(items);
            questionVos.add(questionVo);

        }
        System.out.println("转换好的 qvo：" + questionVos);
        if (paperVo.getTitleItems() != null) {
            System.out.println("不为空");
        }

        //创建一个 大题list
        List<QuestionsVo> questionsVos = new ArrayList();
        System.out.println("查到的 content" + paper.getTitleContent());
        //先去查有多少个大题
        List<TitleContent> titleContents = (List<TitleContent>) JSON.parseArray(paper.getTitleContent(), TitleContent.class);
//        输出一下转换后的 titleContent

//       JSONObject msgEncap = JSON.parseObject(paper.getTitleContent());
//       List<TitleContent> titleContents = JSON.parseArray(msgEncap.getString("msgList"), Record.class);
//

        System.out.println("json转换后的titleContent的长度" + titleContents.size());
        //       设置大题title
        for (TitleContent titleContent : titleContents) {
            System.out.println("遍历输出 titleName" + titleContent.getTitleName());
            QuestionsVo questionsVo = new QuestionsVo();
            questionsVo.setQuestionName(titleContent.getTitleName());
            questionsVo.setType(titleContent.getType());
            questionsVos.add(questionsVo);//将titleContent转为questionsVos
        }


        for (QuestionsVo questionsVo : questionsVos) {
            //把小题塞进大题里面
            List<QuestionVo> questionVo1 = new ArrayList<>();
//           for (int i=0 ; i<questionVos.size();i++){
//               QuestionVo questionVo = questionVos.get(i);
//               System.out.println(questionVo.toString());
//               if (questionsVo.getType() == 1 && (questionVo.getType() == 1 || questionVo.getType() == 2)) {
//                   questionVo1.add(questionVo);//一号大题
//               }
//               if (questionsVo.getType() == questionVo.getType()) {
//                   questionVo1.add(questionVo);//二号大题
//               }
//               if (questionsVo.getType() == questionVo.getType()) {
//                   questionVo1.add(questionVo);//三号大题
//               }
//           }
            for (QuestionVo questionVo : questionVos) {//会重复输入
                System.out.println(questionVo.toString());
                if (questionsVo.getType() == questionVo.getType()) {
                    questionVo1.add(questionVo);//
                }
            }
            questionsVo.setQuestionItems(questionVo1);
//           for (QuestionVo questionVo : questionVos) {
//               //questionsVo是每一道大题  questionVo是大题里面的小题
//
//               questionsVo.getQuestionItems().add(questionVo);
//           }
        }
        for (QuestionsVo questionsVo : questionsVos) {
            System.out.println("输出一下 已经穿件好的大题列表" + questionsVo);
        }
//        最后把每一道大题塞进一张试卷里
        paperVo.setTitleItems(questionsVos);
        return paperVo;
    }

    public int insertPublishPaper(PublishPaper publishPaper) {
        publishPaper.setMarked(1);

        //先查询试卷是否已经发布
       Integer isMarked =  testExamDao.isMarked(publishPaper);
       if(isMarked == null){
           testExamDao.InsertPublishPaper(publishPaper);
           return 1;
       }
        //如果发布就不插入

        return 0;
    }

    public int getTestTime(int paperId) {

        return testExamDao.getTestTime(paperId);
    }
}